package com.wangsh.userscenter.outer.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.common.controller.BaseController;
import com.wangsh.userscenter.common.util.ConstatFinalUtil;
import com.wangsh.userscenter.outer.service.IOuterService;
import com.wangsh.userscenter.users.service.IUsersService;

/**
 * 对外公开的类
 * 
 * @author wangshh
 * 
 */
@Controller
@RequestMapping("/outer/common")
public class CommonHeadOuterController extends BaseController
{
	@Resource
	private IOuterService outerService ;
	@Resource
	private IUsersService usersService;
	
	/**
	 * 查询地区树形结构的JSON
	 * @param request
	 */
	@RequestMapping("/regionList.html")
	public String regionList(HttpServletRequest request,HttpServletResponse response)
	{
		JSONObject resultJSON = new JSONObject() ; 
		String json = request.getParameter("json");
		try
		{
			JSONObject reqJSON = (JSONObject) JSON.parse(json);
			
			if(reqJSON != null)
			{
				String version = reqJSON.getString("version");
				if("1".equalsIgnoreCase(version))
				{
					resultJSON = this.outerService.regionList01Service(reqJSON);
				}
			}else
			{
				//上行参数解析失败
				resultJSON.put("code", "1");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("地区解析上行参数失败了;上行参数:{},",json , e);
		}
		
		this.printOut(request, response, resultJSON.toString());
		return null ;
	}
	
	/**
	 * 验证用户的Ticket
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/verifyUsersToken.html",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String verifyUsersToken(String json) throws Exception
	{
		ConstatFinalUtil.SYS_LOGGER.info("--verfiyToken--");
		JSONObject resultJSON = new JSONObject() ; 
		
		/* 处理参数 */
		if(json == null)
		{
			json = "" ; 
		}
		
		if(!"".equalsIgnoreCase(json))
		{
			/*
			 * 重要,重要,重要
			 * 版本????考虑版本
			 * 
			 * */
			JSONObject reqJSON = new JSONObject() ;
			/* 传了上行参数,解析 */
			try
			{
				reqJSON = (JSONObject) JSON.parse(json);
				
				try
				{
					/* 取到版本 */
					String version = reqJSON.get("version") + "";
					if("1".equalsIgnoreCase(version))
					{
						/* 第一个协议版本 */
						resultJSON = this.outerService.verfiyUsersToken01Service(reqJSON);
					}/*else if("2".equalsIgnoreCase(version))
					{
						 第一个协议版本 
						resultJSON = this.outerService.verfiyToken02Service(reqJSON);
					}*/else
					{
						/* 版本号不正确 */
						resultJSON.put("code", "11");
						resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("11") + "");
					}
				} catch (Exception e)
				{
					resultJSON.put("code", "13");
					resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("13") + ";" + e.toString());
				}
			} catch (Exception e)
			{
				/* 上传的json数据格式不正确 */
				Map<String, String> infoMap = new HashMap<String, String>();
				String resultStr = this.fileUtil.replaceOperator(ConstatFinalUtil.INFO_JSON.get("10") + "", infoMap);
				resultJSON.put("code", "10");
				resultJSON.put("info", resultStr);
			}
		}else
		{
			/* 请上传上行参数 */
			resultJSON.put("code", "9");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("9") + "");
		}
		return resultJSON.toJSONString();
	}
}
